package com.cysion.study.ui.personal

import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.view.View
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.base.BaseFragmentAdapter
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.darkTextTheme
import com.cysion.other.color
import com.cysion.other.startActivity_ex
import com.cysion.targetfun._subscribe
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.R
import com.cysion.study.constant.BUNDLE_KEY
import com.cysion.study.customview.java.SimpleData
import com.cysion.study.helper.UserCache
import com.cysion.study.net.ApiClient
import com.cysion.study.ui.fragment.NoSignInFragment
import com.cysion.study.widget.ShowToast
import kotlinx.android.synthetic.main.activity_attent_student.*
import java.util.*


//今日考勤--学生考勤
class StudentAttentActivity : BaseActivity(),TabLayout.OnTabSelectedListener, View.OnClickListener {



    private lateinit var mFragments: MutableList<Fragment>
    private lateinit var mTitles: MutableList<String>
    //    lateinit var simpleDatas:SimpleData
    override fun getLayoutId(): Int = R.layout.activity_attent_student


    override fun initView() {

        darkTextTheme(color(R.color.white))
        iv_back.setOnClickListener {
            finish()
        }

        vp_content.offscreenPageLimit = 2
        vp_content.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(main_tablayout))

        mFragments=getFragments()
        mTitles = getTitles()
        vp_content.adapter = BaseFragmentAdapter(this.supportFragmentManager, mFragments, mTitles)
        main_tablayout.setupWithViewPager(vp_content)
//        tablayout.tabTextColors = resources.getColorStateList(R.color.main_cl)
        main_tablayout.setSelectedTabIndicatorColor(resources.getColor(R.color.main_cl))

        tv_tag1.setOnClickListener(this)
        tv_tag2.setOnClickListener(this)
        tv_tag3.setOnClickListener(this)
        tv_tag4.setOnClickListener(this)
    }

    override fun initData() {
        super.initData()
        getDataList()
    }

    private fun getDataList() {
        //isSign 是否签到 -1.全部 0.已签到 1.未签到
        ApiClient.api.getOwnClasses(UserCache.token,"-1")
            .compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    inView(it)

                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }
    }

    override fun onClick(v: View?) {
        var bundle = Bundle()
        when (v?.id) {
            R.id.tv_tag1->{
                bundle.putString("type", "1")//1请假,2早走,3晚走,4未续费
                self.startActivity_ex<AttentLeaveActivity>(BUNDLE_KEY, bundle)//请假
            }
            R.id.tv_tag2->{//早走
                bundle.putString("type", "2")//1请假,2早走,3晚走,4未续费
                self.startActivity_ex<AttentLeaveActivity>(BUNDLE_KEY, bundle)
            }
            R.id.tv_tag3->{//晚走
                bundle.putString("type", "3")//1请假,2早走,3晚走,4未续费
                self.startActivity_ex<AttentLeaveActivity>(BUNDLE_KEY, bundle)
            }
            R.id.tv_tag4->{//未续费
                bundle.putString("type", "4")//1请假,2早走,3晚走,4未续费
                self.startActivity_ex<AttentLeaveActivity>(BUNDLE_KEY, bundle)
            }
        }
    }

    private fun inView(it: SimpleData) {
        tv_title.text="今日托管学生"+it.entityNum+"人"

    }


    private fun getTitles(): MutableList<String> {
        val strings = ArrayList<String>()
        strings.add("未签到")
        strings.add("已签到")
        return strings
    }

    private fun getFragments(): MutableList<Fragment> {
        val fragmentArrayList = ArrayList<Fragment>()
        val f1 = NoSignInFragment()
        val f2 = NoSignInFragment()
        val b1 = Bundle()
        val b2 = Bundle()
        // 0.已签到 1.未签到
        b1.putString("isSign", "1")
        f1.arguments = b1
        fragmentArrayList.add(f1)//未签到

        b2.putString("isSign", "0")
        f2.arguments = b2
        fragmentArrayList.add(f2)//已签到
//        fragmentArrayList.add(NoSignInFragment())//已签到
        return fragmentArrayList
    }



    override fun onTabReselected(tab: TabLayout.Tab?) {


    }

    override fun onTabUnselected(tab: TabLayout.Tab?) {
    }

    override fun onTabSelected(tab: TabLayout.Tab?) {
        vp_content.currentItem = tab!!.position
    }


    override fun closeMvp() {
    }


}
